package com.bigfreakindeals.Protocols.SoapHandlers;

import java.io.IOException;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Models.UniversityDeals;
import com.bigfreakindeals.Models.UserProfile;

public class LoginWithFacebookHandler {

	static final String STATUS = "status";

	static Boolean isLogin;

	static final String USER_NAME = "username";
	static final String PASSWORD = "password";
	static final String SESSION_TOKEN = "sessionToken";

	static final String NOTIFICATION = "receive_notification";
	
	static final String NEWDEALS = "receive_new_deals";
	static final String BEANSSAYS = "receive_beans_says";
	static final String REMEMBERME = "receive_remember_me";

	/* user preference university */
	static final String PARENT = "city";
	static final String ID = "id";
	static final String NAME = "name";
	static final String FIRSTNAME = "fname";
	static final String LASTNAME = "lname";
	
	static final String STATE = "state_name";
	static final String SLUG = "slug";
	static final String COUNTRY = "country_name";
	static final String LAT = "latitude";
	static final String LONG = "longitude";
	static final String ISAPPROVED = "is_approved";
	static final String EMAIL = "email";
	static final String ERROR = "error";

	//UserProfile userProfile = new UserProfile();
	String feedUrl;

	private static final String SOAP_ACTION = "1";
	private static final String METHOD_NAME = "bigfreakindeals";
	private static final String NAMESPACE = "1";

	Context context;

	public LoginWithFacebookHandler(String feedUrl, Context context) {
		this.feedUrl = feedUrl;
		this.context = context;
		parse();
	}

	public void parse() {

		try {
			System.out.println("login with facebook handler starts...");
			
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
			envelope.setOutputSoapObject(request);

			HttpTransportSE androidHttpTransport = new HttpTransportSE(feedUrl);

			try {
				androidHttpTransport.call(SOAP_ACTION, envelope);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("android http error");
				// TODO: handle exception
			}

			SoapObject response = (SoapObject) envelope.getResponse();
			System.out.println("response"+response);
			
			String login = response.getProperty(STATUS).toString();
			
			System.out.println("login"+login);
			if (login != null && login.length() > 0) {
				if (login.contentEquals("false")) {
					isLogin = false;
				} else {
					isLogin = true;
				}
			}
			ApplicationEx.getInstance().myProfile.setLogin(isLogin);

			SoapObject userObj = (SoapObject) response.getProperty("user");
			System.out.println("userObj"+userObj);
			
			if (isLogin) {

				// 	
				
				String user = userObj.getProperty(USER_NAME).toString();
				if (user != null && user.length() > 0) {
					ApplicationEx.getInstance().myProfile.setUsername(user);
				}
				
			
				
				String email = userObj.getProperty(EMAIL).toString();
				if (email != null && email.length() > 0) {
					ApplicationEx.getInstance().myProfile.setUsername(email);
				}

				String password = userObj.getProperty(PASSWORD).toString();
				if (password != null && password.length() > 0) {
					ApplicationEx.getInstance().myProfile.setPassword(password);
				}

				String session = response.getProperty(SESSION_TOKEN).toString();
				if (session != null && session.length() > 0) {
			
					ApplicationEx.getInstance().myProfile.setSessionID(session);
					System.out.println("session"+session);
					
				}
				
				try {
					
					Vector apnsDev = (Vector)userObj.getProperty("apns_devices");
					System.out.println("apnsDev"+apnsDev);
					
					for (int count = 0;count <= apnsDev.size() -1; count++){
						
						SoapObject settings = (SoapObject)apnsDev.get(count);
						
						if (settings.getProperty(BEANSSAYS) != null){
							String beansays = settings.getProperty(BEANSSAYS).toString();
							ApplicationEx.getInstance().myProfile.setSettins_notificationbeanSays(Integer.parseInt(beansays));
						}
						
						if (settings.getProperty(NEWDEALS) != null){
							String newdeals = settings.getProperty(NEWDEALS).toString();
							ApplicationEx.getInstance().myProfile.setSettins_notificationNewDeals(Integer.parseInt(newdeals));
						}
						
					}
				} catch (Exception e) {
					// TODO: handle exception
				}

				/* user pref university */
				SoapObject city = (SoapObject) userObj.getProperty("city");
				String lat = city.getProperty(LAT).toString();
				String longi = city.getProperty(LONG).toString();
				String slug = city.getProperty(SLUG).toString();
				
				ApplicationEx.getInstance().myProfile.university_preference_Lat = lat;
				ApplicationEx.getInstance().myProfile.university_preference_Long = longi;
				ApplicationEx.getInstance().myProfile.university_preference_Slug = slug;
				
				
				System.out.println("login with facebook handler ends... CALL EVENT LOGIN SUCCESS");
				//ApplicationEx.getInstance().myProfile = userProfile;
				Intent intent = new Intent(Constant.EVENTS_FACEBOOK_LOGINSUCCESS);

				ApplicationEx.getInstance().sendBroadcast(intent);

				return;

			} else {

				
				String error = response.getProperty(ERROR).toString();
				
				Intent intent = new Intent(Constant.EVENTS_FACEBOOK_LOGINERROR);
				
				intent.putExtra(Constant.TAG_WEBSERVICE_ERROR_MESSAGE, error);
				
				ApplicationEx.getInstance().sendBroadcast(intent);
				
				return;
			}

		} catch (IOException e1) {
			// TODO Auto-generated catch block

			Intent intent = new Intent(Constant.EVENTS_FACEBOOK_LOGINERROR);
			intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
			ApplicationEx.getInstance().sendBroadcast(intent);

		} catch (Exception e1) {

			Intent intent = new Intent(Constant.EVENTS_FACEBOOK_LOGINERROR);
			intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
			ApplicationEx.getInstance().sendBroadcast(intent);
		}

	}
}